package com.tianzeculture.stagebao.activity.home.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.home.adapter.WorkTagAdapter;
import com.tianzeculture.stagebao.bean.Notice;
import com.tianzeculture.stagebao.dialog.DateDialog;
import com.tianzeculture.stagebao.view.MyGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/5 0005.
 * 招募筛选
 */

public class NoticeSearchPopWindow extends PopupWindow implements View.OnClickListener {
    private Activity mContext;
    private final LayoutInflater inflater;
    private View searchView;
    private final Display display;
    private final WindowManager windowManager;
    private NoticeSearchListener noticeSearchListener;

    RadioGroup rgPublishTime;
    RadioButton rbtnBuxian, rbtnOne, rbtnThree, rbtnYear;
    TextView tvStartTime, tvEndTime;
    boolean isStartTime = true;
    RadioButton rbtnMoneyYes, rbtnMoneyNo, rbtnMoneyBuxian;
    RadioButton rbtnClothYes, rbtnClothNo, rbtnClothBuxian;
    RadioButton rbtnSexMale, rbtnSexFemale, rbtnSexBuxian;

    MyGridView gvWorkTag;
    WorkTagAdapter tagAdapter = null;
    List<String> checkedTag = null;

    Button btnReset, btnSure;

    public NoticeSearchPopWindow(Activity context) {
        this.mContext = context;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        searchView = inflater.inflate(R.layout.popwindow_notice_search, null);

        this.setContentView(searchView);
        //设置CityPickerPopWindow弹出窗体的宽
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        this.setWidth(RelativeLayout.LayoutParams.FILL_PARENT);
        //设置CityPickerPopWindow弹出窗体的高
//        this.setHeight((int) (display.getHeight() * 0.35));
        this.setHeight((int) (RelativeLayout.LayoutParams.WRAP_CONTENT));
        //设置CityPickerPopWindow弹出窗体可点击
        this.setFocusable(true);
        //设置CityPickerPopWindow可触摸
        this.setTouchable(true);
        //设置CityPickerPopWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x9000000);
        //设置CityPickerPopWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOutsideTouchable(true);
        setUpListener(searchView);
    }

    List<String> tags = new ArrayList<>();

    private void initTag() {
        tags.clear();
        tags.add("时尚人像");
        tags.add("时装拍摄");
        tags.add("婚纱样片");
        tags.add("汽车广告");
        tags.add("泳装拍摄");
        tags.add("内衣拍摄");
        tags.add("个护化妆");
        tags.add("珠宝配饰");
        tags.add("游戏动漫");
        tags.add("数码产品");
        tags.add("家具用品");
        tags.add("其他用品");
    }

    public void setUpListener(View searchView) {
        rgPublishTime = searchView.findViewById(R.id.rgPublishTime);
        rbtnBuxian = searchView.findViewById(R.id.rbtnBuxian);
        rbtnOne = searchView.findViewById(R.id.rbtnOne);
        rbtnThree = searchView.findViewById(R.id.rbtnThree);
        rbtnYear = searchView.findViewById(R.id.rbtnYear);

        tvStartTime = searchView.findViewById(R.id.tvStartTime);
        tvEndTime = searchView.findViewById(R.id.tvEndTime);
        tvStartTime.setOnClickListener(this);
        tvEndTime.setOnClickListener(this);

        rbtnMoneyYes = searchView.findViewById(R.id.rbtnMoneyYes);
        rbtnMoneyNo = searchView.findViewById(R.id.rbtnMoneyNo);
        rbtnMoneyBuxian = searchView.findViewById(R.id.rbtnMoneyBuxian);

        rbtnClothYes = searchView.findViewById(R.id.rbtnClothYes);
        rbtnClothNo = searchView.findViewById(R.id.rbtnClothNo);
        rbtnClothBuxian = searchView.findViewById(R.id.rbtnClothBuxian);
        rbtnSexMale = searchView.findViewById(R.id.rbtnSexMale);
        rbtnSexFemale = searchView.findViewById(R.id.rbtnSexFemale);
        rbtnSexBuxian = searchView.findViewById(R.id.rbtnSexBuxian);

        btnReset = searchView.findViewById(R.id.btnReset);
        btnSure = searchView.findViewById(R.id.btnSure);

        initTag();
        gvWorkTag = searchView.findViewById(R.id.gvWorkTag);
        tagAdapter = new WorkTagAdapter(mContext, tags);
        gvWorkTag.setAdapter(tagAdapter);

        btnReset = searchView.findViewById(R.id.btnReset);
        btnReset.setOnClickListener(this);

        btnSure = searchView.findViewById(R.id.btnSure);
        btnSure.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvStartTime:
                isStartTime = true;

                DateDialog dateDialog = new DateDialog(mContext, mContext.getString(R.string.start_time), true);

                dateDialog.setOnSelectClickListener(new DateDialog.OnSelectClickListener() {
                    @Override
                    public void onOk(int year, int month, int day) {
                        tvStartTime.setText(year + "-" + month + "-" + day);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
                dateDialog.show();
                break;
            case R.id.tvEndTime:
                isStartTime = false;
                DateDialog dateDialog2 = new DateDialog(mContext, mContext.getString(R.string.end_time), true);

                dateDialog2.setOnSelectClickListener(new DateDialog.OnSelectClickListener() {
                    @Override
                    public void onOk(int year, int month, int day) {
                        tvEndTime.setText(year + "-" + month + "-" + day);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
                dateDialog2.show();
                break;
            case R.id.btnReset:
                cleanAllChecked();
                break;
            case R.id.btnSure:
                saveSearch();
                break;
        }
    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
//            this.showAtLocation(parent, Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
            this.showAsDropDown(parent);

        } else {
            this.dismiss();
        }
    }


    public interface NoticeSearchListener {
        void pickNoticeSearchValue(Notice notice);

        void noticeSearchCancel();
    }

    public void setNoticeSearchListener(NoticeSearchListener listener) {
        this.noticeSearchListener = listener;
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }


    @Override
    public void dismiss() {
        super.dismiss();
        noticeSearchListener.noticeSearchCancel();
    }


    /**
     * 重置所有筛选
     */
    private void cleanAllChecked() {
        rbtnBuxian.setChecked(false);
        rbtnOne.setChecked(false);
        rbtnThree.setChecked(false);
        rbtnYear.setChecked(false);
        rbtnMoneyYes.setChecked(false);
        rbtnMoneyNo.setChecked(false);
        rbtnMoneyBuxian.setChecked(false);
        rbtnClothYes.setChecked(false);
        rbtnClothNo.setChecked(false);
        rbtnClothBuxian.setChecked(false);
        rbtnSexMale.setChecked(false);
        rbtnSexFemale.setChecked(false);
        rbtnSexBuxian.setChecked(false);
        tvStartTime.setText("");
        tvEndTime.setText("");
        isStartTime = true;

        checkedTag = null;
        tagAdapter.setCheckedTag(checkedTag);
    }

    /**
     * 保存筛选条件
     */
    private void saveSearch() {
        Notice notice = new Notice();
        if (rbtnOne.isChecked())
            notice.setAddTime("0");
        else if (rbtnThree.isChecked())
            notice.setAddTime("1");
        else if (rbtnYear.isChecked())
            notice.setAddTime("2");
        else
            notice.setAddTime("");

        if (!"".equals(tvStartTime.getText().toString().trim())) {
            notice.setStratTime(tvStartTime.getText().toString().trim());
        }
        if (!"".equals(tvEndTime.getText().toString().trim())) {
            notice.setEndTime(tvEndTime.getText().toString().trim());
        }

        if (rbtnMoneyYes.isChecked())
            notice.setIstravel("1");
        else if (rbtnMoneyNo.isChecked())
            notice.setIstravel("0");
        else
            notice.setIstravel("");

        if (rbtnClothYes.isChecked())
            notice.setIscostume("1");
        else if (rbtnClothNo.isChecked())
            notice.setIscostume("0");
        else
            notice.setIscostume("");

        if (rbtnSexMale.isChecked())
            notice.setSex("1");
        else if (rbtnSexFemale.isChecked())
            notice.setSex("0");
        else
            notice.setSex("2");

//        notice.setWorkNote(checkedTa);

        if (checkedTag == null)
            checkedTag = new ArrayList<>();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < checkedTag.size(); i++) {
            buffer.append(checkedTag.get(i) + ",");
        }
        if (buffer.toString().length() > 0) {
            String ss = buffer.toString().substring(0, buffer.toString().length() - 1);
            notice.setWorkNote(ss);
        }

        noticeSearchListener.pickNoticeSearchValue(notice);
        dismiss();
    }
}
